<?php 

namespace App\Services;

use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Models\API\VerificationCode;
use App\Services\AliyunSMS\SmsService;

class VerificationCodeService {

    // 获取sql数据
	private function search($phone)
    {
    	return VerificationCode::where('phone', $phone)->first();
    }

    // 生成验证码
    private function createCode()
    {
    	return mt_rand(000000, 999999);
    }

    // 检查是否可以再次获取 90秒后
    private function checkSecond($updated_at)
    {
        $oldTime = strtotime($updated_at);

        $now = time();

        $num = $now - $oldTime;

        if ($num < 90) {
            return true;
        }
    }

    // 储存验证码
    private function store($phone)
    {
    	$code = $this->createCode();
    	$verificationCode = new VerificationCode;
    	$verificationCode->phone = $phone;
    	$verificationCode->verification_code = $code;
        $verificationCode->created_at = Carbon::now();
        $verificationCode->updated_at = Carbon::now();
        if ($this->sendCode($phone, $code)) {
            if ($verificationCode->save()) {
                $msg = '验证码发送成功';
                return $this->setJson(true, $msg, 200);
            } else {
                $msg = $this->setError('服务器错误,未能发送!');
                return $this->setJson(false, $msg, 500);
            }
        } else {
            $msg = $this->setError('服务器错误,未能发送!');
            return $this->setJson(false, $msg, 500);
        }
    }

    // 更新验证码
    private function update($verificationCode, $phone)
    {
        $updated_at = $verificationCode->updated_at;
        if ($this->checkSecond($updated_at)) {
            $msg = $this->setError('验证码已发送，请90s后重试');
            return $this->setJson(false, $msg, 422);
        }
    	$code = $this->createCode();
    	$verificationCode->phone = $phone;
    	$verificationCode->verification_code = $code;
        $verificationCode->updated_at = Carbon::now();
        if ($this->sendCode($phone, $code)) {
            if ($verificationCode->save()) {
                $msg = '验证码发送成功';
                return $this->setJson(true, $msg, 200);
            } else {
                $msg = $this->setError('服务器错误,未能发送!');
                return $this->setJson(false, $msg, 500);
            }
        } else {
            $msg = $this->setError('服务器错误,未能发送!');
            return $this->setJson(false, $msg, 500);
        }
    }

    // 规范返回格式
    private function setJson($status, $msg, $code)
    {
        $res = [
            'status' => $status,
            'msg'    => $msg,
            'code'   => $code
        ];
        return $res;
    }
    
    // 调用第三方发送短信
    private function sendCode($phone, $code)
    {
        $paramString = json_encode(['code' => ''.$code]);
        SmsService::send('大橙子', 'SMS_59945394', $phone, $paramString);
        return true;
    }

    // 检查有效期 10分钟|600s
    public function check($phone, $resouestCode)
    {
		$verificationCodeObj = $this->search($phone);

        if (count($verificationCodeObj) === 0) {
            $msg = $this->setError('请点击 获取验证码');
            return $this->setJson(false, $msg, 422);
        }

    	$oldTime = strtotime($verificationCodeObj->updated_at);

    	$now = time();

    	$num = $now - $oldTime; // 秒数

    	// 验证码有效时长10分钟,600s
    	if ($num > 600) {
    		$msg = $this->setError('验证码过期，请重新获取');
            return $this->setJson(false, $msg, 422);
    	}

        $code = $verificationCodeObj->verification_code;

        if ($code == $resouestCode) {
            return ['status' => true];
        } else {
            $msg = $this->setError('验证码错误请重新输入');
            return $this->setJson(false, $msg, 422);
        }
    }

    // 发送
    public function send($phone)
    {
        $verificationCode = $this->search($phone);

        if (count($verificationCode) === 0) {
            return $this->store($phone);
        } else {
            return $this->update($verificationCode, $phone);
        }
    }

    public function setError($error)
    {
        return ['verification_code' => $error];
    }
}